/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.formats.input.transactions;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Stack;
import java.util.ArrayList;
import java.math.BigDecimal;
import com.citep.business.Account;
import com.citep.business.AccountTypes;
import com.citep.business.sessions.AccountSession;
import com.citep.business.sessions.TransactionSession;
import com.citep.business.AccountCategory;
import com.citep.formats.input.ImportException;
import com.citep.formats.input.ImportFileTypes;
import com.citep.formats.input.transactions.*;
import java.io.*;

public class TransactionImportContext {
	

		private TransactionSession transactionSession;

		public TransactionImportContext(TransactionSession transactionSession) {
			this.transactionSession = transactionSession;
		}
		
		
				
		private void processTransactions() {
		}


		public ArrayList<ImportedStatement> readSource(DataInputStream stream, ImportFileTypes type) throws ImportException {
			TransactionImportStrategy strategy;
			
			if(type == ImportFileTypes.OFX1_STATEMENT)
				strategy = new OFXImport(stream);
			else
				throw new ImportException("TransactionImportContext: I don't know how to handle streams of type: " + type);
			
			final ArrayList<ImportedStatement> statements = new ArrayList<ImportedStatement>();
			
			strategy.setStatementHandler(new StatementImportSink() {
				public void onImportStatement(ImportedStatement s) {
					System.out.println("Importing statement:" + s);
					statements.add(s);
				}
				
				public void onDocumentEnd() { }			
			});
			
			strategy.start();
			return statements;
		}
		
		
		public static void main(String [] args) throws ImportException, FileNotFoundException {
			TransactionImportContext importContext = new TransactionImportContext(null);
			DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("/home/lurker/workspace32bit/OFX/export.ofx")));
			
			ArrayList<ImportedStatement> statements = importContext.readSource(in, ImportFileTypes.OFX1_STATEMENT);
			for(ImportedStatement s:statements) {
				System.out.println("Account: " + s.getAccount());
				for(ImportedEntry e:s.getEntries()) {
					System.out.println(":" + e);
				}
					
			}
		}
}
